<?php
class Administrator_award_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }	
	
	function monitor_update()
	{
		$username = $this->session->userdata('username');
		$cat_at = $this->input->post('cat_at');
		$old_score = $this->DB_AWARD->get('award_vote_score',1)->row()->{''.$cat_at.''};
		$new_score = $this->input->post('score');
		//Update Score
		$this->DB_AWARD->update('award_vote_score',array(''.$cat_at.'' => $new_score));
		//Insert Update Score
		$data = array(
			'cat_at' => $cat_at,
			'from_score' => $old_score,
			'to_score' => $new_score,
			'update_by' => $username,
			'create_date' => date('Y-m-d h:i:s')
		);
		$this->DB_AWARD->insert('award_update_score',$data);
	}
	
	function ajax_load_score()
	{
		$dataResult = $this->DB_AWARD->get('award_vote_score',1)->row();
		echo json_encode($dataResult);
		exit;
	}
	
	function comment_refresh()
	{
		//Load library Facebook
		$this->load->library('Fb');
		//SetUp
		$facebook = new Facebook(array(
			'appId'  => $this->config->item('fb_api'),
			'secret' => $this->config->item('fb_secret')
		));		
		
		$comments_cat_fbid_arr = array();
		/*Cat 01*/
		$comments_cat_fbid_arr[] = array(
			'1436920233192709', //01/1
			'692264547461787', //01/2
			'334704426667264', //01/3
			'643855508998873', //01/4
			'572558629487354', //01/5
			'771030489579157' //01/6
		);
		/*Cat 02*/
		$comments_cat_fbid_arr[] = array(
			'710910488921064', //02/1
			'1389527391297837', //02/2
			'634797339899693', //02/3
			'1439197829626997', //02/4
			'4478079686544777', //02/5
			'243384635822159' //02/6
		);
		/*Cat 03*/
		$comments_cat_fbid_arr[] = array(
			'566697290067806', //03/1
			'614635465269082', //03/2
			'408891019244846', //03/3
			'184092951785167', //03/4
			'205875856262171', //03/5
			'224405227740964' //03/6
		);
		/*Cat 04*/
		$comments_cat_fbid_arr[] = array(
			'569164743159511', //04/1
			'238680986299064', //04/2
			'374623152673928', //04/3
			'697744589288201', //04/4
			'719962878023133', //04/5
			'1375300459394128' //04/6
		);
		/*Cat 05*/
		$comments_cat_fbid_arr[] = array(
			'578377408898341', //05/1
			'721232037911037', //05/2
			'189608854577572', //05/3
			'238976309596398', //05/4
			'609627265753614', //05/5
			'1384148341805731' //05/6
		);
		/*Cat 06*/
		$comments_cat_fbid_arr[] = array(
			'757396610940973', //06/1
			'713170712036089', //06/2
			'341044786037362', //06/3
			'187202611481797', //06/4
			'635502493163114', //06/5
			'176777225866747' //06/6
		);
		/*Cat 07*/
		$comments_cat_fbid_arr[] = array(
			'591572824248672', //07/1
			'638282619565824', //07/2
			'444768002290687', //07/3
			'555114157911931', //07/4
			'190624931128704', //07/5
			'274770582692726' //07/6
		);
		/*Cat 08*/
		$comments_cat_fbid_arr[] = array(
			'1441831119366460', //08/1
			'728487670513483', //08/2
			'1500784326812892', //08/3
			'1447522625469192', //08/4
			'591152884289157', //08/5
			'237217819786141' //08/6
		);
		/*Cat 09*/
		$comments_cat_fbid_arr[] = array(
			'1463918367168378', //09/1
			'237053963131034', //09/2
			'347404892064029', //09/3
			'595178690555586', //09/4
			'587441871334964', //09/5
			'406638909469006' //09/6
		);
		/*Cat 10*/
		$comments_cat_fbid_arr[] = array(
			'248785051954299', //10/1
			'232845753560722', //10/2
			'188505098010483', //10/3
			'257904937700697', //10/4
			'532679466829402', //10/5
			'245474662285302' //10/6
		);
		/*Cat 11*/
		$comments_cat_fbid_arr[] = array(
			'595187153869994', //11/1
			'623029011089339', //11/2
			'1422943881275694', //11/3
			'432416660193542', //11/4
			'269449049869668', //11/5
			'617375234993784' //11/6
		);
		/*Cat 12*/
		$comments_cat_fbid_arr[] = array(
			'249669248528858', //12/1
			'631268516935690', //12/2
			'1448429762043986', //12/3
			'186159338246264', //12/4
			'227515374087634', //12/5
			'707431372609575' //12/6
		);
		
		foreach($comments_cat_fbid_arr as $cat_id => $fb_id_arr):
			$multiQuery = array(
				'comment' => "SELECT object_id,post_fbid, fromid, id, object_id, text, likes, time FROM comment WHERE object_id IN (".implode(',',$fb_id_arr).") ORDER BY likes DESC",
				'site' => "SELECT id,url FROM object_url WHERE id IN (".implode(',',$fb_id_arr).")"
			);

			$multiQueryResult = $facebook->api(array(
			    'method'   => 'fql.multiquery',
			    'queries'  => $multiQuery
			));

			$all_comment = $multiQueryResult[0]['fql_result_set'];
			$all_site = $multiQueryResult[1]['fql_result_set'];

			//Set Site
			$site = array();
			foreach($all_site as $value):
				$site[$value['id']] = $value['url'];
			endforeach;

			//Set Comment
			$dataInsert = array();
			$dataUser = array();

			foreach($all_comment as $key => $value):
				//if 100 then break
				if($key > 99) break;
				$is_exist = $this->DB_AWARD->from('award_comment')->where(array('comment_id' => $value['post_fbid']))->count_all_results();
				if(!empty($is_exist)):
					if(in_array($value['fromid'],$dataUser)):
						if($value['likes'] <= $dataUser[$value['fromid']]):
							continue; //Skip
						else:
							$dataUser[$value['fromid']] = $value['likes'];
						endif;
					endif;
					$is_exist = $this->DB_AWARD->from('award_comment')->where(array('comment_id' => $value['post_fbid'],'likes' => $value['likes']))->count_all_results();
					if(empty($is_exist)): //if likes not equal then update
						$dataUpdate = array(
									'message' => $value['text'],
									'url' => $site[$value['object_id']],
									'likes' => $value['likes'],
									'fb_time' => date('Y-m-d h:i:s',$value['time'])			
								);
						$this->DB_AWARD->update('award_comment',$dataUpdate,array('comment_id' => $value['post_fbid']));
					endif;
				else:
					if(in_array($value['fromid'],$dataUser)):
						if($value['likes'] <= $dataUser[$value['fromid']]):
							continue; //Skip
						else:
							$dataUser[$value['fromid']] = $value['likes'];
						endif;
					endif;	
					$dataInsert[] = array(
						'comment_id' => $value['post_fbid'],
						'user_id' => $value['fromid'],
						'cat_id' => intval($cat_id+1),
						'message' => $value['text'],
						'url' => $site[$value['object_id']],
						'likes' => $value['likes'],
						'fb_time' => date('Y-m-d h:i:s',$value['time'])		
					);
				endif;
			endforeach;
			if(!empty($dataInsert)) $this->DB_AWARD->insert_batch('award_comment',$dataInsert);		
		endforeach;	
	}
	
	function comment_update()
	{
		$check_box = $this->input->post('check_box');
		//Clear All Active
		$this->DB_AWARD->update('award_comment',array('is_active' => '0'));
		if(!empty($check_box)) foreach($check_box as $value):
			$this->DB_AWARD->update('award_comment',array('is_active' => '1'),array('comment_id' => $value));
		endforeach;
	}	
}

?>